package service

import (
	"context"
	v1 "grpc-demo/api/product/v1"
	"grpc-demo/app/grpc-demo-server/internal/biz"
)

var _ v1.ProductServer = (*ProductService)(nil)

type ProductService struct {
	v1.UnimplementedProductServer
	pc *biz.ProductUseCase
}

func (p *ProductService) AddProduct(ctx context.Context, req *v1.AddProductReq) (*v1.AddProductReply, error) {

	product, err := p.pc.AddProduct(ctx, &biz.Product{
		Name:        req.Name,
		Description: req.Description,
		Price:       req.Price,
	})

	if err != nil {
		return nil, err
	}

	return &v1.AddProductReply{
		Id: product.Id,
	}, nil
}

func (p *ProductService) GetProduct(ctx context.Context, req *v1.GetProductReq) (*v1.GetProductReply, error) {

	product, err := p.pc.GetProduct(ctx, &biz.Product{
		Id:          req.Id,
	})

	if err != nil {
		return nil, err
	}

	return &v1.GetProductReply{
		Id:          product.Id,
		Name:        product.Name,
		Description: product.Description,
		Price: product.Price,
	}, nil
}

func NewProductService(pc *biz.ProductUseCase) *ProductService {
	return &ProductService{
		pc: pc,
	}
}
